/*
 * Copyright (C) 2008 ProSyst Software GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.modulefusion.dirinstaller;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Arrays;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator {

	private static final String DIRINSTALLER_POLL = "dirinstaller.poll";
	private static final String DIRINSTALLER_DIR = "dirinstaller.dir";
	private static final String DIRINSTALLER_EXCLUDE = "dirinstaller.exclude";
	
	private DirWatcher watcher;
	
	public void start(BundleContext context) throws Exception {
		String bundlesDir = context.getProperty(DIRINSTALLER_DIR);
		bundlesDir = bundlesDir == null ? "load" : bundlesDir;
		
		String intervalStr = context.getProperty(DIRINSTALLER_POLL);
		Integer interval = intervalStr == null ? 2000 : Integer.valueOf(intervalStr);
		
		final String[] excludes = (context.getProperty(DIRINSTALLER_EXCLUDE)==null ? "" : context.getProperty(DIRINSTALLER_EXCLUDE)).split("[,]");
		
        watcher = new DirWatcher(context, bundlesDir, interval, new DirInstallerFileFilter(excludes));
		watcher.startWatching();
	}

	public void stop(BundleContext context) throws Exception {
		watcher.stopWatching();
	}

	private static class DirInstallerFileFilter implements FileFilter {
        
	    private final String[] excludes;
	    
	    public DirInstallerFileFilter(String[] excludes) {
	        this.excludes=excludes;
	    }
	    
        /**
         * @see java.io.FileFilter#accept(java.io.File)
         */
        public boolean accept(File pathname) {
            if ((excludes==null) || (excludes.length==0) || ((excludes.length==1) && (excludes[0].length()==0))) {
                return true;
            }
            String canonicalPath;
            try {
                canonicalPath = pathname.getCanonicalPath();
            } catch (IOException e) {
                e.printStackTrace();
                return true;
            }
            for (String currentExclude : excludes) {
                if (canonicalPath.indexOf(currentExclude)!=-1) {
                    return false;
                }
            }
            return true;
        }

        /**
         * @see java.lang.Object#hashCode()
         */
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + Arrays.hashCode(excludes);
            return result;
        }

        /**
         * @see java.lang.Object#equals(java.lang.Object)
         */
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            DirInstallerFileFilter other = (DirInstallerFileFilter) obj;
            if (!Arrays.equals(excludes, other.excludes))
                return false;
            return true;
        }

        /**
         * @see java.lang.Object#toString()
         */
        @Override
        public String toString() {
            StringBuffer b=new StringBuffer();
            for (String currentExclude : excludes) {
                b.append(currentExclude).append(",");
            }
            return b.substring(0, b.length()-1);
        }
    };
}
